<?php
/**
 * This file is part of Notadd.
 *
 * @author        Hollydan <2642956839@qq.com>
 * @copyright (c) 2017, notadd.com
 * @datetime      17-11-21 下午3:56
 */

namespace Notadd\Seckill\GraphQL\Mutations;


use GraphQL\Type\Definition\Type;
use Notadd\Foundation\GraphQL\Abstracts\Mutation;
use Notadd\Foundation\Validation\Rule;
use Notadd\Seckill\Models\Seckill;

class SeckillCreateMutation extends Mutation
{

    /**
     * @return array
     */
    public function args(): array
    {
        return [
            'name' => [
                'name' => 'name',
                'rules' => Rule::required(),
                'type' => Type::string(),
                'description' => '活动名称',
            ],
            'date' => [
                'name' => 'date',
                'rules' => Rule::required(),
                'type' => Type::string(),
                'description' => '活动日期',
            ],
            'spus' => [
                'name' => 'spus',
                'rules' => Rule::required(),
                'type' => Type::string(),
                'description' => '参与活动的商品',
            ],
            'useful' => [
                'name' => 'useful',
                'rules' => Rule::numeric(),
                'defaultValue' => 1,
                'type' => Type::int(),
                'description' => '活动是否启用',
            ],
            'timeBlockId' => [
                'name' => 'time_block_id',
                'rules' => [
                    Rule::required(),
                    Rule::numeric(),
                    Rule::in('ext_seckill_time_blocks'),
                ],
                'type' => Type::int(),
                'description' => '活动时间段',
            ],
        ];
    }

    /**
     * @param $root
     * @param $args
     *
     * @return mixed
     */
    public function resolve($root, $args)
    {
        if (Seckill::query()->create($args)) {
            return json_encode([
                'code' => 200,
                'message' => '活动信息添加成功',
            ]);
        } else {
            return json_encode([
                'code' => 500,
                'message' => '活动信息添加失败',
            ]);
        }
    }

    /**
     * @return \GraphQL\Type\Definition\ListOfType
     */
    public function type()
    {
        return $this->graphql->type('Seckill');
    }
}